﻿// ---------------------------------------------------------------------
// Partial của lớp SSupervisor
//
// Nội dung:
//    + Các hàm xử lý lúc nhận được message từ client trong lượt Draw Phase
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Yugi.Client.Card;
using System.Threading;
using ManagementEffect;
using ServerYuGi.PythonRegister;


namespace ServerYuGi
{
    public partial class SSupervisor
    {
        void Process_GAME_CHIABAIXONG(params object[] param)
        {
            SendMessage_TURN_DRAWPHASE_BEGIN_ToAll(TurnManager.IDPlayerTrongLuot);
            DangKyScript_ChiaBaiXong();
            DrawPhase();  
        }

        void DangKyScript_ChiaBaiXong()
        {
            CardScript script = null;
            foreach (SPlayer player in Players)
            {
                //  [4/23/2010 TinNguyen] - Fix Loi Add Store
                this.ExEffectManager.IDPlayerCheck = player.ID;
                foreach(SCard item in player.SoBaiTrenTay)
                {
                    script = CScriptDB.GetScript(item.MyYBaseCard.Card_TypeID);
                    if (script != null)
                        if (script.IsCheck_Hand)
                            //  [4/23/2010 TinNguyen] Previous : DangKiSript(item); - Fix Loi CheckType
                            DangKiSriptBatDauVan(item);
                }
            }
        }

        /// <summary>
        /// Bắt đầu drawphase
        /// </summary>
        public void DrawPhase()
        {
            int IDPlayerRutBai = TurnManager.IDPlayerTrongLuot;
            SPlayer player = LayPlayer(IDPlayerRutBai);             
            SCard tmp = player.RutLaBai();
            if(tmp == null)
            {
                SPlayer OtherPlayer = LayPlayerKhacVoiID(IDPlayerRutBai);
                Console.WriteLine("\n\nEnd Game:     ID Win: " + OtherPlayer.ID.ToString() + "    ID Lose: " + player.ID.ToString());
                logManager.Save();
                SendMessage_GAME_END_ToAll(OtherPlayer.ID, player.ID);
                SendMessage_GAME_RESET_ToAll();
                F5();
                return;
            }
            
            SendMessage_CARD_RUTBAI_ToID(IDPlayerRutBai, tmp.MyYBaseCard.Card_TypeID, tmp.MyYBaseCard.ID);
            SendMessage_CARD_RUTBAI_DOITHU_ToID(LayPlayerKhacVoiPlayerDangTrongLuot().ID);
            logManager.LogRutBai(tmp.MyYBaseCard.ID, IDPlayerRutBai);

            ExEffectManager.IDPlayerCheck = TurnManager.IDPlayerTrongLuot;
            ExEffectManager.CheckPointNow = YCheckPoint.DRAWCARD;
            ExEffectManager.Init();            
            CardScript script = CScriptDB.GetScript(tmp.MyYBaseCard.Card_TypeID);
            if(script!=null)
                if(script.IsCheck_Hand)
                    DangKiSript(tmp);
            
            RevMSGmanager.isPlayerKoTrongLuot = true;
            List<YCheckPoint> checkpoint = new List<YCheckPoint> { YCheckPoint.DRAWCARD };
            Check_EffectExcute(checkpoint, ExEffectManager.IDPlayerCheck, "api_manager,id_rut", api_manager, tmp.MyYBaseCard.ID);
            RevMSGmanager.Init();
        }

        void Process_CARD_RUTBAI_XONG(params object[] param)
        {
            TurnManager.NextTurn();
        }
       
    }
}
